#include "videocapturedetectthread.h"

VideoCaptureDetectThread::VideoCaptureDetectThread(QObject *parent ) : DetectThread(parent) {}

void VideoCaptureDetectThread::init(std::shared_ptr<Inference> inference1,
                                    bool run_with_cuda,bool islabel, ThreadSafeQueue *matQueue2)
{

this->inference=inference1;
    this->islabel=islabel;
    this->RunWithCuda=run_with_cuda;
    //this->matQueue=matQueue2;
}

void VideoCaptureDetectThread::stop()
{
    STOP = true;
    qDebug()<<"VideoCaptureDetectThread::stop";
    //    cap.release();
    if(cap.isOpened()){
        qDebug()<<"VideoCapture  release   " ;
        cap.release();
    }

}

void VideoCaptureDetectThread::do_detect()
{


    cap.open(0);
    if (!cap.isOpened())
        return;
    int idx = 0;
    qDebug()<<"VideoCaptureDetectThread STOP 1 "<<STOP;
    auto radio_button = islabel;
    while (!STOP )
    {
        idx++;
        // 少取一个 ?
        if (idx % 2 == 0) {
            idx = 0;
            continue;
        }

        cv::Mat frame;
        cap >> frame;
        if (frame.empty())
            return;
        QString msg= this->inference->doPredict(frame, radio_button);
        emit onMessage(msg);
        QPixmap map = commons::cvMatToQPixmap(frame);
        //this->main_window->show_img(map);
        // 等 33
        emit imageReady(map);
        // cv::waitKey(33);
        cv::waitKey(100);
        // if (!frame.empty()) {
        //     matQueue->push(frame.clone());
        // }

        // QPixmap map = commons::cvMatToQPixmap(frame);
        //this->main_window->show_img(map);
    }
    qDebug()<<"VideoCaptureDetectThread release end STOP 2 "<<STOP;
    //cap.release();
    qDebug()<<"VideoCaptureDetectThread release end STOP 3 "<<STOP;
}

void VideoCaptureDetectThread::release()
{
    if(cap.isOpened()){
        qDebug()<<"VideoCapture  release   " ;
        cap.release();
    }

}

int VideoCaptureDetectThread::get_detect_id()
{
    return DetectThread::CameraDetectThreadID;
}
